package com.tools.cleanmaster.ui.view;

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.TouchDelegate;
import android.view.View;
import android.widget.ImageView;

/**
 * 增加点击热区
 * Created by lianzhan on 15/12/29.
 */
public class ImageViewDelegate extends ImageView {
    //增大响应热区。
    private int mAdditionWidthPadding = 50;

    private int mPreviousLeft = -1;
    private int mPreviousRight = -1;
    private int mPreviousBottom = -1;
    private int mPreviousTop = -1;

    public ImageViewDelegate(Context context) {
        super(context);
    }

    public ImageViewDelegate(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ImageViewDelegate(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right,
                            int bottom) {
        super.onLayout(changed, left, top, right, bottom);

        if (left != mPreviousLeft || top != mPreviousTop
                || right != mPreviousRight || bottom != mPreviousBottom) {
            mPreviousLeft = left;
            mPreviousTop = top;
            mPreviousRight = right;
            mPreviousBottom = bottom;
            final View parent = (View) this.getParent();
            parent.setTouchDelegate(new TouchDelegate(
                    new Rect(left - mAdditionWidthPadding, top, right + mAdditionWidthPadding, bottom), this));

        }
    }
}
